<?php
/**
 * Minhlog 搜索页面功能控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Frontend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: SearchController.php 28 2010-03-11 15:28:53Z minh.shu $
 *
 */

class SearchController extends Minh_Controller_Action
{
    protected $_index = null;
    protected $_articleModel;

    public function init()
    {
        if(null === $this->_index)
        {
            if(SEARCH_ENGINE == 'Sphinx')
            {
                $this->_index = Minh_Search_Sphinx::getInstance();
            } else
            {
                $this->_index = new Minh_Search_Lucene();
            }
        }

        if(null === $this->_articleModel)
        {
            if(class_exists('Domain_Model_Article'))
            {
                $this->_articleModel = new Domain_Model_Article();
            } else
            {
                throw new Exception('文章模型类不存在!');
            }
        }

        parent::init();
    }

    public function keywordAction()
    {
        $filter = new Zend_Filter_StripTags();
        $keyword = strtolower(trim($filter->filter($this->_getParam('keyword'))));

        $redirectUrl = '/search/'.urlencode($keyword).'-1.html';
        $this->_redirect($redirectUrl);
    }

    public function articleAction()
    {
        $filter = new Zend_Filter_StripTags();
        $keyword = strtolower(trim($filter->filter(urldecode($this->_getParam('keyword')))));

        $page = $filter->filter($this->_getParam('page', 1));

        $hits = $this->_index->search($keyword);

        if(empty($hits))
        {
            $this->_redirect('/index.html');
        } else {
            if(SEARCH_ENGINE == 'Sphinx')
            {
                $where = new stdClass();
                $where->type = 'search';
                $where->value = $hits;
                $resultSet = $this->_articleModel->getAllArticle($where);
                foreach($resultSet as $articleSet)
                {
                    $articleSet->article->publishTime = date('Y-m-d', $articleSet->article->publishTime);
                }
            } else {
                $resultSet = array();
                foreach($hits as $hit)
                {
                    $articleSet = new stdClass();
                    $articleSet->article->articleId = (int) $hit->articleId;
                    $articleSet->article->articleTitle = $hit->articleTitle;
                    $articleSet->article->articleSummary = $hit->articleSummary;
                    $articleSet->article->publishTime = date('Y-m-d', $hit->publishTime);
                    $articleSet->article->clickCount = (int) $hit->clickCount;
                    $articleSet->article->commentCount = (int) $hit->commentCount;
                    $articleSet->category->categoryId = (int) $hit->categoryId;
                    $articleSet->category->categoryName = $hit->categoryName;
                    $tagIds = explode(',',$hit->tagId);
                    $tagNames = explode(',',$hit->tag);
                    foreach($tagIds as $k => $tagId)
                    {
                        $tag = new stdClass();
                        $tag->tagId = $tagId;
                        $tag->tagName = $tagNames[$k];
                        $articleSet->tags[] = $tag;
                        unset($tag);
                    }
                    $resultSet[] = $articleSet;
                    unset($articleSet);
                }
            }

            $perPageNum = 6;
            $articleList = Zend_Paginator::factory($resultSet);
            $articleList->setCurrentPageNumber($page)->setItemCountPerPage($perPageNum);
        }

        //SEO 信息
        if($page > 1) $this->view->headTitle()->append('Part '.$page);
        $this->view->headTitle($keyword, 'PREPEND');
        $this->view->headMeta()->appendName('keywords', $keyword);
        $this->view->headMeta()->appendName('description', $keyword . '关键词相关文章 - Part '.$page);

        $this->view->keyword = $keyword;
        $this->view->articleList = $articleList;

        $this->fetchExecuteTime();

        echo $this->view->render('list.phtml');
    }
}